import App from './App'
import store from '@/common/store.js'

// Element Plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 全局导入样式
import './common/main.css'
import './common/element-plus.css'
import './common/animate.min.css'

import func from '@/common/func.js'
import db from './common/db.js'
import config from '@/manifest.json'

let api = {
	offset: function(a) {
		var b, st;
		if (document.documentElement) {
			b = document.documentElement.scrollLeft;
			st = document.documentElement.scrollTop
		} else {
			b = document.body.scrollLeft;
			st = document.body.scrollTop
		}
		var c = a.getBoundingClientRect();
		return {
			c,
			l: c.left + b,
			t: c.top + st,
			w: a.offsetWidth,
			h: a.offsetHeight
		}
	},
	get_form_prop: function(obj) {
		let _name = ''
		for (let _key in obj) {
			if (obj[_key] === '') {
				_name = _key
				break
			}
		}
		return _name
	},
	// 设置未验证 form
	set_unvalidated_form_focus: function(_this, obj) {
		let _name = this.get_form_prop(obj)
		
		if (!_name || !_this.$refs[_name]) return
		
		try {
			_this.$refs[_name].$el.querySelector("input").focus()
			_this.$refs[_name].$el.querySelector("input").click()
		} catch (error) {
			try {
				try {
					_this.$refs[_name].choose()
				} catch (error) {
					_this.$refs[_name].$el.querySelector("textarea").focus()
				}
			} catch (error) {
				_this.$refs[_name].$children[1].$el.querySelector('.w-e-text').focus()
			}
		} finally {
			_this.$refs[_name].$el.scrollIntoViewIfNeeded(true)
		}
	},
	// 滚动至存储的位置
	scrollToStorage: function(selector, path) {
		const body = document.querySelector(selector)
		let scrollTop = 0
		if (body) {
			scrollTop = uni.getStorageSync(`__scroll${path}`)
			body.scrollTop = scrollTop
		}
	},
	// 过滤特殊字符
	filterRegexpKey: function(str) {
		if (!str) return '';
	
		str += ''
	
		const arr = ['\\', '$', '(', ')', '*', '+', '.', '[', ']', '?', '^', '{', '}', '|']
	
		arr.forEach(x => {
			str = str.replace(new RegExp(`\\${x}`), `\\${x}`);
		})
	
		return str
	},
	// 返回 manifest.json 配置项
	config() {
		return config
	}
}

// // #ifndef VUE3
// import Vue from 'vue'
// import './uni.promisify.adaptor'
// Vue.config.productionTip = false
// App.mpType = 'app'
// const app = new Vue({
//   ...App
// })
// app.$mount()
// // #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
	// 注册图标
	for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
	  app.component(key, component)
	}
	// 全局配置
	app.config.globalProperties.$api = api
	app.config.globalProperties.$func = func
	app.config.globalProperties.$db = db
	app.config.globalProperties.$udb = uniCloud.database()
	app.use(ElementPlus)
	app.use(store)
  return {
    app
  }
}
// #endif